﻿@{
    Layout = null;
}
<section class="content-header">
    <h1>
        快捷登录
    </h1>
    <ol class="breadcrumb">
        <li><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i> 首页</a></li>
        <li class="active">用户</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-lg-12">
            <div class="box box-primary">

                <div class="box-body no-padding">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped table-hover dataTable text-center">
                            <thead>
                                <tr>
                                    <th>OAuth</th>
                                    <th>App Id</th>
                                    <th>App Key</th>
                                    <th>可用</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>QQ</td>
                                    <input type="hidden" name="oauthData[id][qq]" />
                                    <td><input type="text" value="" name="oauthData[appId][qq]" style="width: 200px" class="input-text"></td>
                                    <td><input type="text" value="" name="oauthData[appKey][qq]" style="width: 300px" class="input-text"></td>
                                    <td>
                                        <div class="checker"><span class="checked"><input type="checkbox" name="oauthData[isEnable][qq]"></span></div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>微信</td>
                                    <input type="hidden" name="oauthData[id][weixin]" />
                                    <td><input type="text" value="" name="oauthData[appId][weixin]" style="width: 200px" class="input-text"></td>
                                    <td><input type="text" value="" name="oauthData[appKey][weixin]" style="width: 300px" class="input-text"></td>
                                    <td>
                                        <div class="checker"><span class="checked"><input type="checkbox" name="oauthData[isEnable][weixin]"></span></div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>微博</td>
                                    <input type="hidden" name="oauthData[id][sina]" />
                                    <td><input type="text" value="" name="oauthData[appId][sina]" style="width: 200px" class="input-text"></td>
                                    <td><input type="text" value="" name="oauthData[appKey][sina]" style="width: 300px" class="input-text"></td>
                                    <td>
                                        <div class="checker"><span class="checked"><input type="checkbox" name="oauthData[isEnable][sina]"></span></div>
                                    </td>
                                </tr>

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <div class="modal-footer">
        @Html.Bootstrap().Button("保存", "btnSave").Style(BootstrapStyle.Primary)
    </div>
</section>
<script type="text/javascript">
    $(function () {
        $.ajax({
            type: "POST",
            url: '@Url.Action("GetOauthList")',
            dataType: "json",
            success: function(data) {
                if (!data) {
                    return;
                }
                var qqOauth = {}, weixinOauth = {}, sinaOauth = {};
                for (var d in data) {
                    switch (data[d].interface) {
                    case 1:
                        qqOauth = data[d];
                        break;
                    case 2:
                        weixinOauth = data[d];
                        break;
                    case 3:
                        sinaOauth = data[d];
                        break;
                    }
                }

                var oauthData = {
                    id: {
                        qq: qqOauth.id,
                        weixin: weixinOauth.id,
                        sina: sinaOauth.id
                    },
                    appId: {
                        qq: qqOauth.appId,
                        weixin: weixinOauth.appId,
                        sina: sinaOauth.appId
                    },
                    appKey: {
                        qq: qqOauth.appKey,
                        weixin: weixinOauth.appKey,
                        sina: sinaOauth.appKey
                    },
                    isEnable: {
                        qq: qqOauth.isEnable,
                        weixin: weixinOauth.isEnable,
                        sina: sinaOauth.isEnable
                    }
                };

                for (var d in oauthData) {
                    for (var o in oauthData[d]) {
                        var $domObj = $("input[name='oauthData[" + d + "][" + o + "]']");
                        if ($domObj.size() > 0) {
                            if (d == 'isEnable') {
                                $domObj.attr("checked", oauthData[d][o]);
                            } else {
                                $domObj.val(oauthData[d][o]);
                            }

                        }
                    }
                }
            }
        });
    });


    $('#btnSave')
        .click(function() {
            var oauthData = {
                id: {
                    qq: $("input[name='oauthData[id][qq]']").val(),
                    weixin: $("input[name='oauthData[id][weixin]']").val(),
                    sina: $("input[name='oauthData[id][sina]']").val()
                },
                appId: {
                    qq: $("input[name='oauthData[appId][qq]']").val(),
                    weixin: $("input[name='oauthData[appId][weixin]']").val(),
                    sina: $("input[name='oauthData[appId][sina]']").val()
                },
                appKey: {
                    qq: $("input[name='oauthData[appKey][qq]']").val(),
                    weixin: $("input[name='oauthData[appKey][weixin]']").val(),
                    sina: $("input[name='oauthData[appKey][sina]']").val()
                },
                isEnable: {
                    qq: $("input[name='oauthData[isEnable][qq]']").is(':checked'),
                    weixin: $("input[name='oauthData[isEnable][weixin]']").is(':checked'),
                    sina: $("input[name='oauthData[isEnable][sina]']").is(':checked')
                }
            };

            var arrOauth = [];
            arrOauth.push({
                "interface": 1,
                "id": oauthData.id.qq,
                "appId": oauthData.appId.qq,
                "appKey": oauthData.appKey.qq,
                "isEnable": oauthData.isEnable.qq
            });
            arrOauth.push({
                "interface": 2,
                "id": oauthData.id.weixin,
                "appId": oauthData.appId.weixin,
                "appKey": oauthData.appKey.weixin,
                "isEnable": oauthData.isEnable.weixin
            });
            arrOauth.push({
                "interface": 3,
                "id": oauthData.id.sina,
                "appId": oauthData.appId.sina,
                "appKey": oauthData.appKey.sina,
                "isEnable": oauthData.isEnable.sina
            });

            console.log(arrOauth);
            $.ajax({
                type: "POST",
                url: '@Url.Action("SaveOauthList")',
                data: { dtoModelList: arrOauth },
                dataType: "json",
                success: function(data) {
                    if (data.errors.length > 0) {
                        swal("出错啦！", data.errors, "error");
                    } else {
                        swal("已经保存", "保存成功！", "success");
                    }
                }
            });

            //var that = $('#btnSave');
            //that.removeClass("disabled").removeAttr("disabled").val('保存');
        });

</script>